#!/usr/bin/env python3
"""
创建专业的LimeReader应用图标
设计理念：专业、简洁、有深度的文档查看器形象
符合Apple设计美学，强调扁平化中的层次感和柔和的视觉体验
"""

from PIL import Image, ImageDraw, ImageFilter
import math
import os

def create_squircle_mask(size, corner_radius_ratio=0.2):
    """创建Apple风格的超椭圆(Squircle)蒙版"""
    mask = Image.new('L', (size, size), 0)
    draw = ImageDraw.Draw(mask)

    # 计算圆角半径，接近边缘时逐渐变小
    radius = int(size * corner_radius_ratio)

    # 使用多层圆角矩形来模拟超椭圆效果
    for i in range(10):
        factor = 1.0 - (i * 0.02)  # 逐渐缩小
        current_radius = int(radius * factor)
        offset = i

        # 绘制圆角矩形
        coords = [offset, offset, size - offset - 1, size - offset - 1]
        draw.rounded_rectangle(coords, radius=current_radius, fill=255)

    return mask

def create_document_icon(size):
    """根据设计方案创建文档图标"""
    # 创建画布
    img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)

    # 1. 背景 - 柔和的石板蓝色
    # 选择深沉、冷静的灰蓝色
    bg_color = (70, 90, 120, 255)  # 石板蓝

    # 创建超椭圆背景
    mask = create_squircle_mask(size, 0.22)
    bg_img = Image.new('RGBA', (size, size), bg_color)
    bg_img.putalpha(mask)

    # 先绘制背景
    img = Image.alpha_composite(img, bg_img)

    # 2. 文档元素设置
    # 文档大小和位置
    doc_margin = size * 0.15
    doc_width = size - 2 * doc_margin
    doc_height = doc_width * 1.2  # 文档比例

    # 确保文档不超出画布
    if doc_height > size - 2 * doc_margin:
        doc_height = size - 2 * doc_margin
        doc_width = doc_height / 1.2

    # 文档位置（居中偏上）
    doc_x = (size - doc_width) // 2
    doc_y = size * 0.12

    # 3. 辅助文档（背景文档）
    aux_offset_x = size * 0.03
    aux_offset_y = size * 0.03
    aux_doc_coords = [
        doc_x + aux_offset_x,
        doc_y + aux_offset_y,
        doc_x + doc_width + aux_offset_x - 2,
        doc_y + doc_height + aux_offset_y - 2
    ]

    # 绘制辅助文档阴影
    shadow_offset = 2
    shadow_coords = [
        aux_doc_coords[0] + shadow_offset,
        aux_doc_coords[1] + shadow_offset,
        aux_doc_coords[2] + shadow_offset,
        aux_doc_coords[3] + shadow_offset
    ]
    draw.rounded_rectangle(shadow_coords, radius=3, fill=(0, 0, 0, 30))

    # 绘制辅助文档
    draw.rounded_rectangle(aux_doc_coords, radius=3, fill=(245, 245, 245, 255))
    draw.rounded_rectangle(aux_doc_coords, radius=3, outline=(200, 200, 200, 100), width=1)

    # 4. 主文档
    main_doc_coords = [doc_x, doc_y, doc_x + doc_width - 2, doc_y + doc_height - 2]

    # 主文档阴影（更明显）
    main_shadow_coords = [
        main_doc_coords[0] + 3,
        main_doc_coords[1] + 3,
        main_doc_coords[2] + 3,
        main_doc_coords[3] + 3
    ]
    draw.rounded_rectangle(main_shadow_coords, radius=4, fill=(0, 0, 0, 50))

    # 绘制主文档
    draw.rounded_rectangle(main_doc_coords, radius=4, fill=(255, 255, 255, 255))
    draw.rounded_rectangle(main_doc_coords, radius=4, outline=(210, 210, 210, 150), width=1)

    # 5. 页角卷起效果
    corner_size = size * 0.08
    corner_x = main_doc_coords[2] - corner_size
    corner_y = main_doc_coords[1]

    # 卷起的三角形
    corner_points = [
        (main_doc_coords[2], main_doc_coords[1]),
        (corner_x, main_doc_coords[1]),
        (main_doc_coords[2], corner_y + corner_size)
    ]
    draw.polygon(corner_points, fill=(240, 240, 240, 255))
    draw.polygon(corner_points, outline=(200, 200, 200, 100), width=1)

    # 6. 文档内容 - 双栏文字线条
    content_margin = size * 0.05
    content_x = doc_x + content_margin
    content_y = doc_y + content_margin
    content_width = doc_width - 2 * content_margin
    content_height = doc_height - 2 * content_margin

    # 计算栏宽
    column_gap = size * 0.03
    column_width = (content_width - column_gap) // 2

    # 文字线条颜色
    text_color = (120, 120, 120, 255)
    title_color = (90, 90, 90, 255)

    # 左栏内容
    left_x = content_x
    line_height = size * 0.025
    line_spacing = size * 0.008

    # 左栏标题
    title_height = size * 0.03
    draw.rectangle([left_x, content_y, left_x + column_width * 0.8, content_y + title_height],
                  fill=title_color)

    # 左栏文字行
    current_y = content_y + title_height + line_spacing * 2
    for i in range(int((content_height - title_height - line_spacing * 3) // (line_height + line_spacing))):
        if current_y + line_height > doc_y + doc_height - content_margin:
            break

        # 变化行长度模拟真实文字
        line_width = column_width * (0.7 + 0.3 * ((i % 3) / 3))
        draw.rectangle([left_x, current_y, left_x + line_width, current_y + line_height],
                      fill=text_color)
        current_y += line_height + line_spacing

    # 右栏内容
    right_x = content_x + column_width + column_gap

    # 右栏标题
    draw.rectangle([right_x, content_y, right_x + column_width * 0.7, content_y + title_height],
                  fill=title_color)

    # 右栏文字行
    current_y = content_y + title_height + line_spacing * 2
    for i in range(int((content_height - title_height - line_spacing * 3) // (line_height + line_spacing))):
        if current_y + line_height > doc_y + doc_height - content_margin:
            break

        # 变化行长度模拟真实文字
        line_width = column_width * (0.6 + 0.4 * ((i % 4) / 4))
        draw.rectangle([right_x, current_y, right_x + line_width, current_y + line_height],
                      fill=text_color)
        current_y += line_height + line_spacing

    # 7. 侧边指示器
    indicator_count = 4
    indicator_width = size * 0.03
    indicator_height = size * 0.02
    indicator_spacing = size * 0.015
    indicator_color = (200, 120, 80, 255)  # 柔和的橙色

    # 指示器起始位置
    indicators_total_height = indicator_count * indicator_height + (indicator_count - 1) * indicator_spacing
    indicator_start_y = doc_y + (doc_height - indicators_total_height) // 2
    indicator_x = doc_x - indicator_width - size * 0.01

    # 绘制指示器
    for i in range(indicator_count):
        y_pos = indicator_start_y + i * (indicator_height + indicator_spacing)
        draw.rounded_rectangle([
            indicator_x, y_pos,
            indicator_x + indicator_width, y_pos + indicator_height
        ], radius=2, fill=indicator_color)

    return img

def create_all_icon_sizes():
    """生成所有需要的图标尺寸"""
    sizes = [16, 32, 48, 64, 128, 256, 512]

    # 确保icons目录存在
    icons_dir = "/Users/ethan/Codes/longimagereader/icons"
    os.makedirs(icons_dir, exist_ok=True)

    print("开始生成新的专业图标...")

    # 生成主图标
    main_icon = create_document_icon(512)
    main_icon.save(os.path.join(icons_dir, "app.png"), "PNG")
    print("✓ 生成主图标 app.png")

    # 生成各种尺寸
    for size in sizes:
        print(f"生成 {size}x{size} 图标...")
        if size == 512:
            icon = main_icon
        else:
            icon = main_icon.resize((size, size), Image.Resampling.LANCZOS)

        filename = f"app_{size}x{size}.png"
        icon.save(os.path.join(icons_dir, filename), "PNG")
        print(f"✓ 生成 {filename}")

    print("\n所有PNG图标生成完成！")
    return main_icon

if __name__ == "__main__":
    create_all_icon_sizes()